package com.chengxiaoxiao.libarypartner.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.LoadDialogUtils;
import com.chengxiaoxiao.libarypartner.utils.UserUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONObject;

public class PersonActivity extends Activity
{

    private TextView textRole;
    private TextView textName;
    private TextView textGender;
    private TextView textStuNumber;
    private TextView textNumber;
    private TextView textClasses;
    private TextView textAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);

        textRole = (TextView) findViewById(R.id.textRole);
        textName = (TextView) findViewById(R.id.textName);
        textGender = (TextView) findViewById(R.id.textGender);
        textStuNumber = (TextView) findViewById(R.id.textStuNumber);
        textNumber = (TextView) findViewById(R.id.textNumber);
        textClasses= (TextView) findViewById(R.id.textClasses);
        textAddress = (TextView) findViewById(R.id.textAddress);

        LoadDialogUtils.show(PersonActivity.this,"","加载中...");
        loadInfo();

    }

    public void back(View v)
    {
        finish();

    }

    private void loadInfo()
    {
        final String path = ApiAddress.getApiDomain() + "/client/ClientServlet?op=getUserInfo&uid=" + UserUtils.getNumber(PersonActivity.this);


        AsyncHttpClient client = new AsyncHttpClient();
        client.get(path, new AsyncHttpResponseHandler()
        {

            @Override
            public void onFailure(int statusCode, Header[] arg1, byte[] arg2, Throwable arg3)
            {
LoadDialogUtils.hidden();
            }

            @Override
            public void onSuccess(int arg0, Header[] arg1, byte[] responseBody)
            {
                String body = new String(responseBody);

                try
                {

                    JSONObject obj = new JSONObject(body);

                    int isTeacher = obj.getInt("isTeacher");
                    int borrowNum = obj.getInt("borrowNum");

                    String realName = obj.getString("realName");
                    String classes = obj.getString("classes");
                    String gender = obj.getString("gender");
                    String stuNum = obj.getString("stuNum");
                    String address = obj.getString("address");

                    LoadDialogUtils.hidden();

                    textRole.setText(isTeacher==0?"学生":"老师");
                    textAddress.setText(address);
                    textClasses.setText(classes);
                    textGender.setText(gender);
                    textNumber.setText(borrowNum+"");
                    textName.setText(realName);
                    textStuNumber.setText(stuNum);


                } catch (Exception e)
                {
                    // TODO: handle exception
                }

            }
        });


    }


}
